package com.example.mengxuegu.config;

import com.example.mengxuegu.conponets.MyLocaleResolver;
import com.example.mengxuegu.intercepter.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @program: springboot-mengxuegu
 * @description: 增加视图控制
 * @author: XianJin.Lu
 * @create: 2021-08-19 12:08
 **/



@Configuration
public class MyspringbootMvcConfig implements WebMvcConfigurer {


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        /*增加视图解析*/
        registry.addViewController("/").setViewName("main/login");
        registry.addViewController("/login.html").setViewName("main/login");
        registry.addViewController("/main/index").setViewName("main/index");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor())
                .addPathPatterns("/**")
                //排除不需要拦截的请求路径   注意login  是登录页面检查密码的controller
                .excludePathPatterns("/", "/login.html","/login","/test/**")
                //springboot2+之后需要将静态资源文件的访问路径 也排除
                .excludePathPatterns("/css/*", "/img/*","/js/*");
    }

    /*注入Bean的方法二 */
    //区域解析器
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }
}
